// pages/room5/roomIndex/roomIndex.js
import room4TestData from "../../testData/testR4Data";
import bottomEnterTestData from "../../testData/testBottomEnterData";
var mfApi = require('../../../api/api.js');
const app = getApp();
Page({

        /**
         * 页面的初始数据
         */
        data: {
                baseUrl: app.globalData.baseUrl,
                localRoomData: room4TestData,

                /**
                 * 标记是否被锁
                 * 0 完全锁住
                 * 1 一半锁住
                 * 2 不锁
                 */
                stampLockState: 0,

                picIndex: 1,
                picCurrentIndex: 0,
                /**
                 * 底部入口数据
                 */
                bottomEnterData: bottomEnterTestData,
                currentRoomIndex: app.globalData.gCurrentRoomIndex,
                /**
                 * 0 未获取  1 已获取
                 */
                roomUserData: {},

                // 徽章tip
                isShowTipMask: false,

                // 显示获取一个盲盒提示
                isShowGetBoxView: false,
                // 是否开启开盒动画
                isBoxAniDoing: false,
                // 是否在加载雪碧图
                isBoxImageLoading: false,
                // 开盒动画是否播放结束，避免用户多次、再次点击启动开盒
                isBoxAniFinish: false,
                // 盲盒序列帧
                boxAniFrameImage: '',

                // 是否显示我的证书按钮
                showMineBtn: false,

                // 返回键
                top: 0,
                h: 0 //胶囊高度
        },
        
        onShareTimeline:function(){
 
        },
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                console.log(options)

                this.setData({
                        currentRoomIndex: app.globalData.gCurrentRoomIndex,
                        top: app.menu.top,
                        h: app.menu.height
                })
        },

        async onShow() {
                await this.getStoryDataApi();
                if (!this.data.isShowGetBoxView)
                        await this.getStoryBoxApi();
        },

        /**
         * 启动开盒动画
         */
        startBoxAni() {
                app.saveTrack(`R5-打开盲盒`);
                const aniView = this.selectComponent("#frameAniView"); //组件的id
                if (!this.data.isBoxAniFinish) {
                        aniView.setPlay();
                        this.setData({
                                isBoxAniDoing: true,
                                isBoxAniFinish: false,
                        })
                }
        },

        /**
         * 开盒动画结束
         */
        boxAmiEnd() {
                console.log("boxAmiEnd -=-=-")
                this.setData({
                        isBoxAniDoing: false,
                        isBoxAniFinish: true
                })
        },

        /**
         * 盲盒页关闭
         */
        onGetBoxCloseClick() {
                this.setData({
                        isShowGetBoxView: false,
                        isBoxImageLoading: false,
                })
        },

        onStampIconClick() {
                let temp = this.data.isShowTipMask;
                this.setData({
                        isShowTipMask: !temp
                })
        },

        onToCreateClick() {
                wx.navigateTo({
                        url: '/pages/room5/storyCommitPage/storyCommitPage',
                })
        },

        onToMyClick() {
                wx.navigateTo({
                        url: '/pages/room5/storyForMinePage/storyForMinePage',
                })
                app.saveTrack(`R5-我的证书`);
        },

        async getStoryDataApi() {
                wx.showLoading({
                        title: "加载中",
                });
                let storyRes = await mfApi.http(mfApi.getStoryList(app.globalData.openId), "GET", {});
                if (storyRes.code != 200) {
                        wx.hideLoading();
                        this.setData({
                                showMineBtn: false
                        })
                        return;
                };

                this.setData({
                        showMineBtn: storyRes.result && (storyRes.result.length > 0)
                })

                let listRes = await mfApi.http(
                        mfApi.getRoomList(app.globalData.openId), "GET", {});
                if (listRes.code != 200)
                        return;

                console.log("getRoomUserInfoApi   ===  ", listRes)
                let roomInfo = listRes.result.roomList.find(e => {
                        return e.roomNo == this.data.currentRoomIndex;
                })
                app.globalData.needFirstStoryTip = roomInfo.markFlag == '0';
                this.setData({
                        roomUserData: roomInfo
                })

                let tempEnterData = this.data.bottomEnterData;
                tempEnterData.forEach(element => {
                        // element.index
                        let roomInfo = listRes.result.roomList.find(e => {
                                return e.roomNo == element.enterIndex;
                        });
                        if (roomInfo)
                                element.unlockFlag = roomInfo.unlockFlag;
                })

                this.setData({
                        bottomEnterData: tempEnterData
                })

                wx.hideLoading();
        },

        async getStoryBoxApi() {
                // 查看是否有盲盒
                let res = await mfApi.http(mfApi.getBoxInfo(app.globalData.openId), "GET", {});
                if (res.code != 200 || res.result == null) {
                        // 没有盲盒
                        this.setData({
                                isShowGetBoxView: false,
                                isBoxImageLoading: false,
                        })
                        return;
                };

                // 有盲盒 提示有盲盒 其实此时已经完成了抽盲盒 即使用户不点开查看盲盒 也已经消费了盲盒机会
                console.log("getStoryBoxApi 1212 101010101010")
                this.setData({
                        isShowGetBoxView: true,
                        isBoxImageLoading: true,
                        isBoxAniDoing: false,
                        isBoxAniFinish: false,
                        boxAniFrameImage: res.result.spiritImageUrl
                })
                const indexView = this.selectComponent("#frameAniView"); //组件的id
                indexView.showImageLoading();
        },

        onVideoUpdate(e) {
            if (e.detail.currentTime > 0.1) {
              this.setData({
                picCurrentIndex: 0
              })
            }
          },

        goBack() {
                var hour = new Date().getHours();
                let Back_Img_Type = 1;
                if (hour > 17 || hour < 6) {
                        Back_Img_Type = 3; //1是早上，2是中午，3是晚上
                } else if (hour > 5 && hour < 12) {
                        Back_Img_Type = 1;
                } else {
                        Back_Img_Type = 2;
                }

                this.setData({
                    picIndex:Back_Img_Type,
                    picCurrentIndex:this.data.currentRoomIndex
                });
                
                // 1-ROOM1.jpg
                let tempVideo = `${app.globalData.baseExitVideoUrl}/${Back_Img_Type}-ROOM${this.data.currentRoomIndex}.mp4`;
                let tempPoster = '';

                this.setData({
                        isShowExitVideo: true,
                        exitVideoUrl: tempVideo,
                        exitVideoPosterUrl: tempPoster
                })
        },

        /**
         * 退出视频结束
         */
        onExitVideoEnded() {
                wx.navigateBack({
                        delta: 1,
                })
        },

        /**
         * 底部入口点击
         */
        onBottomEnterClick(e) {
                console.log(e.detail)
                app.globalData.indexTurnDoor = e.detail;
                this.goBack();
        },

        // bind:onBoxLoadFinish="onBoxLoadFinish"
        // bind:onBoxLoadError="onBoxLoadError"
        onBoxLoadFinish() {
                console.log("onBoxLoadFinish  01010011010");
                this.setData({
                        isBoxImageLoading: false
                })
        },

        onBoxLoadError() {
                console.log("onBoxLoadError  0998345942758")
                this.setData({
                        isBoxImageLoading: false
                })
        },

        onShareAppMessage(object) {
                return {
                        title: 'LA MER邀你一同参展，探寻属于你的自愈空间',
                        path: `/pages/index/index?inChannel=commonShare`,
                        imageUrl: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show/share/wx-share.jpg",
                }
        }
})